var base_url = "http://baich-day.com/";

function email_valid()
{
    var str = document.myform.email.value;
    var str2 = "@ucp.edu.pk";
    var mail = str.concat(str2);
  
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      
    if(re.test(mail))
    {
        return_val = response();
             

    }
    else
    {
        var u= "<div class='alert alert-danger' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Invalid Email</div>";
        document.getElementById("result").innerHTML=u;
        return_val = false;
        
    }
    return return_val;
} 

function response()
            {
                var str1 = document.myform.email.value;
                if(str1.lenght==0)
                {
                   document.getElementById("result").innerHTML="";
                   return;

                }
                var str2 = "@ucp.edu.pk";
                var str = str1.concat(str2);

	
                xmlhttp = new XMLHttpRequest();

                xmlhttp.onreadystatechange = function()
                {
                   if(xmlhttp.readyState==4 && xmlhttp.status==200)
                   { 
                        var u = xmlhttp.responseText;
                        document.getElementById("result").innerHTML=u;
                       
                        if(xmlhttp.responseText.trim()=="<div class='alert alert-danger' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Email is not Available</div>")
                        {
                            return_value =false;
                        }
                        else
                        {	
                            return_value =true;
                        }

                   } 
                   else if(xmlhttp.status==404)
                   {
                        document.getElementById("cheker").innerHTML="File not found";
                   }

                }

                xmlhttp.open('GET', "accountController/check_user_ava?user="+str,true);
                xmlhttp.send();
                
               return return_value;
            }
     
    

               
                $("#btn").click(function(){
                
                var em = $('#mail').val();
                var pass = $('#pass').val();
                    
                    
                 $.post(base_url+"accountController/sign_in/",
                    {
                        email : em,
                        pass : pass
                    })
                 .error(
                    function(){
                        console.log("Error!");
                    })
                .success(
                        function(data){
                         //insert into db 
                            
                            if(data.trim()=="<div class='alert alert-danger' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Incorrect Email / Password</div>") {  
                          $( "#con" ).html( data );
                            }
                            else{
                                 window.location.assign(base_url+"/members/")
                                
                            }
                    });
            });

     
     
    